﻿#include "Game_01.hpp"

//#define _DEBUG
#ifdef CONTROL_BY_MOUSE

#endif

const char filename[] = "loc.txt";
extern "C" {
#include <math.h>
}
gf_stage game_stage;

int Setup(void)
{
	FILE* fp = nullptr;

#ifdef _DEBUG
	initConsole();
#endif

	initWindow("Game_01",
		100, 100,
		WIDTH, HEIGHT);

	registerMouseEvent(&Listener::mouseEvent);
	registerTimerEvent(&Listener::timerEvent);
	registerKeyboardEvent(&Listener::keyboardListener);

	beginPaint();
	paintText(0, 0, "CLICK TO START");
	endPaint();

	do {
		if (fopen_s(&fp, filename, "r") == 0)
			break;
		if (fopen_s(&fp, filename, "w"))
		{
			printf(
			"received non-zero when trying to enter %s\n",
			filename);
			break;
		}
		fprintf_s(fp,
			"this is the root location of program %s.\n",
			__FILE__);
		fprintf_s(fp,"DATE:%s",__DATE__);
		fclose(fp);
	} while (0);
	
	fortInstance.angle=90;

	game_stage = PREPARING;

	return 0;
}
void Listener::mouseEvent(int x, int y, int button, int event)
{
#ifdef CONTROL_BY_MOUSE
	int dx = x - base.x, dy = y - base.y;
	double R = sqrt(dx * dx + dy * dy);
#endif
	beginPaint();

	switch (game_stage)
	{
	case PREPARING:
		if (event != BUTTON_DOWN)
			JUMP_OUT;
		game_stage = STARTED;
		break;

	default:
		break;

	}
	startTimer(0,GAME::intervals[0]);
#ifdef CONTROL_BY_MOUSE
	setBrushColor(WHITE);
	setPenColor(WHITE);
	rectangle(0, 0, WIDTH, HEIGHT);
	setPenColor(BLACK);
	setPenWidth(GUN_SIZE);
	line(base.x, base.y,
		base.x + (int)(dx / R * GUN_LENGTH),
		base.y + (int)(dy / R * GUN_LENGTH)
	);
#ifdef  _DEBUG
	setPenWidth(1);
	setPenStyle(PEN_STYLE_DOT);
	line(base.x, base.y, x, y);

#endif
#endif

	endPaint();

}

//double to_radian(int deg)
//{
//	return M_PI*deg/360;
//}
